package com.bsdk.install.device.electricmeter.v1;


import com.alibaba.fastjson2.JSONObject;
import com.bsdk.common.core.domain.AjaxResult;
import com.bsdk.common.utils.SpringContextUtil;
import com.bsdk.install.device.IProductId;
import com.bsdk.install.device.LockBase;
import com.bsdk.install.device.constants.LockConstants;
import com.bsdk.install.device.gateway.GatewayCommand;
import com.bsdk.install.device.gateway.v1.cmd.ANALYSIS;
import com.bsdk.install.device.gateway.v1.cmd.COMMAND;
import com.bsdk.install.queue.PublicQueue;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class DeviceCmd extends LockBase implements GatewayCommand,IProductId {

    String GateWay_Cmd = PID_GATEWAY_WIFI + m_Nomal_Module_cmd; //对于网关，这个很重，PID_GATEWAY_WIFI下发后，会把这个参数带回来
    private JSONObject deviceCommand = new JSONObject();
    COMMAND cmd = new COMMAND();

    public DeviceCmd(String devId, String mac) {

        this.setcMac(mac);
        this.setDevId(devId);
        this.deviceCommand.put("devId", this.getDevId());
        this.deviceCommand.put("cMac", this.getcMac());

        this.deviceCommand.put("uId", String.valueOf(LockConstants.COUNTER.getAndIncrement()));

    }

    public DeviceCmd() {
    }
    @Override
    public void prepareJasonHead()
    {
        this.deviceCommand.put("cMac", "");//发个网关的数据CMac为空
        this.deviceCommand.put("cmd", GateWay_Cmd); //"000080"
    }

    @Override
    public AjaxResult Gw_get_Mqtt()
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_get_Mqtt());
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_set_Mqtt(String m_Devid, String m_User, String m_Pw, String m_Ip, int m_Port)
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_set_Mqtt(m_Devid,m_User,m_Pw,m_Ip,m_Port));
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_get_Server()
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_get_Server());
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_set_Server(String auth_url)
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_set_Server(auth_url));
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_Req_Info()
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_Req_Info());
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_set_Factory(String m_devid, String m_apikey, String m_chipID)
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_set_Factory(m_devid,m_apikey,m_chipID));
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_Req_Factory()
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_Req_Factory());
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_OTA_Upgrade(String str_url)
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_OTA_Upgrade(str_url));
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult Gw_Reset_System()
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_Reset_System());
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult  Gw_Set_Time()
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_Set_Time());
        return AjaxResult.success(this.deviceCommand);
    }
    public AjaxResult  Gw_Get_Time()
    {
        prepareJasonHead();
        this.deviceCommand.put("data", cmd.Gw_Get_Time());
        return AjaxResult.success(this.deviceCommand);
    }
    @Override
    public JSONObject result(JSONObject jsonObject) {

        return (new ANALYSIS()).dealJsonData(jsonObject);

    }

    /**
     * 放入mqtt推送队列中
     *
     * @param jsonObject
     * @return
     */
    @Override
    public AjaxResult send(JSONObject jsonObject) {

        SpringContextUtil.getBean(PublicQueue.class).setDown(jsonObject);

        if (PublicQueue.MAPRESULTQUEUE.containsKey(jsonObject.getString("uId"))) {
            JSONObject jsonObject1 = null;
            try {
                jsonObject1 = (JSONObject) PublicQueue.MAPRESULTQUEUE.get(jsonObject.getString("uId")).take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return AjaxResult.success(jsonObject1);

        } else {
            return AjaxResult.error("结果队列不存在");
        }

    }

}
